#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""  ktrash
 *  Copyright (c) 2008 Dmitry K <drgolova@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 """

from ktrash_ui import KTrashUI
from qt import *
import string
import sys
import os
import splitfile
import trash

blocker=0
split=0
parts=0
filepath=''
filefull=''
filename=''
block_size=0
size=0

class KTrash(QApplication):
    
    def __init__(self, args):
        QApplication.__init__(self,args)
        self.maindialog = TestApp(None)
        self.setMainWidget(self.maindialog)
        self.maindialog.show()
        self.exec_loop()     

class TestApp(KTrashUI):
	
	def __init__(self,parent):
		KTrashUI.__init__(self,parent)
		self._connectSlots()
		
		
	def _connectSlots(self):
		self.connect(self.open_button,SIGNAL("clicked()"),self._slotOpen)
		self.connect(self.send_button,SIGNAL("clicked()"),self._slotSend)
		self.connect(self.path_edit,SIGNAL("textChanged(const QString &)"),self._slotChange)
		self.send_button.setEnabled(0)
	
	def _slotOpen(self):
		global blocker
		global split
		global parts
		global filename
		global filepath
		global filefull
		global block_size
		global size
		text=''
		if not blocker:
			text = QFileDialog.getOpenFileName("", "All files (*.*)", self, "FileDialog")
			
		else:
			self.open_button.setText(u"Открыть")
		blocker=0
		if len(text):
			self.path_edit.setText(text)
		#	blocker=1
		#	self.open_button.setText(u"Обновить")
		if (self.path_edit.text().length()>0):
			text=self.path_edit.text()
		if  os.path.exists(text):
			self.send_button.setEnabled(1)
			#(filepath, filename) = os.path.split(text)
			filefull=text
			size=os.path.getsize(text)			
			str1 = u'Информация: <font color="#007700">%s</font> байт. '%size
			parts=size/(800.0*1024.0*1024.0)
			parts=round(parts)
			if parts>1:
				str1+= u'Файл будет разделен на <b>%d</b> часть(-ей)'%parts			
				split=1
			
		else:
			str1=u'Информация: <font color="#ff0000">ФАЙЛ НЕ НАЙДЕН</font>'			
			split=0
			self.send_button.setEnabled(0)
		self.info_text.setText(str1)

	def _slotSend(self):
		global filefull, filename, filepath, block_size, size
		user_name=self.login_edit.text()
		user_pass=self.pass_edit.text()
		prog='%s'%self.name_edit.text()
		src='%s'%self.src_edit.text()
		vers='%s'%self.version_edit.text()
		descr='%s'%self.info_edit.text()
		cat_name='%s'%self.catalog.currentItem()
		string.replace(descr,u"\r",u" ")
		
		descr=descr.encode("cp1251")
		src=src.encode("cp1251")
		vers=vers.encode("cp1251")
		
		abc='abcdefghijklmnopqrstuvwxyz'
		(filepath,filename)=os.path.split(str(filefull))
		self.setEnabled(0)
		if split:
			splitfile.split(str(filefull),int(parts))

			for i in range(int(parts)):
				file_path='xa%s'%abc[i]
				file_name='%s-%s'%(filename,file_path)
				prog='%s part # %s'%(self.name_edit.text(),str(i))
				prog=prog.encode("cp1251")
				#print descr,src,vers,prog
				trash.upload(file_path,file_name,user_name,user_pass,prog,src,vers,descr,cat_name)

		else:
			file_path=filefull
			file_name=filename
			prog=prog.encode("cp1251")
			#print descr,src,vers,prog
			trash.upload(file_path,file_name,user_name,user_pass,prog,src,vers,descr,cat_name)
		
		self.setEnabled(1)	
		
	def _slotChange(self):
		global blocker
		blocker=1
		self.open_button.setText(u"Обновить")		

if __name__ == "__main__":
    app = KTrash(sys.argv)